package org.groovyflow.spring.web;

import groovy.lang.Binding;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PartialsBuilderWrapper {
	private PartialsBuilder partialsBuilder;
	private Binding binding;
	
	public PartialsBuilderWrapper(PartialsBuilder partialsBuilder, 
			HttpServletRequest req,
			HttpServletResponse res){
		this.partialsBuilder = partialsBuilder;
		this.binding = new Binding();
		binding.setVariable("request", req);
		binding.setVariable("session", req.getSession());
		binding.setVariable("response", res);
	}
	
	public String make(String uri){
		return this.partialsBuilder.make(uri, this.binding);
	}
	public String make(String uri, Binding moreBinding){
		return this.partialsBuilder.make(uri, addToBinding(moreBinding));
	}	
	public String make(String uri, Map moreBinding){
		return this.partialsBuilder.make(uri, addToBinding(moreBinding));
	}	
	
	public Binding addToBinding(Map more){
		for(Iterator iter = more.entrySet().iterator(); iter.hasNext(); ){
			Entry entry = (Entry) iter.next();
			this.binding.setVariable((String) entry.getKey(), entry.getValue());
		}
		return this.binding;
	}
	
	public Binding addToBinding(Binding more){
		return addToBinding(more.getVariables());
	}
	

}
